package com.agh.portal.web;

import java.util.ArrayList;
import java.util.List;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.agh.portal.domain.AghGroup;
import com.agh.portal.domain.Course;
import com.agh.portal.domain.Faculty;
import com.agh.portal.domain.util.SortTool;

@RequestMapping("/faculties")
@Controller
@RooWebScaffold(path = "faculties", formBackingObject = Faculty.class)
public class FacultyController {
	
    @RequestMapping(value = "/{id}", produces = "text/html")
    public String show(
    		@PathVariable("id") Long id, 
    		@RequestParam(value = "page", required = false) Integer page, 
    		@RequestParam(value = "size", required = false) Integer size, 
    		@RequestParam(value = "sortFieldName", required = false) String sortFieldName,
    		@RequestParam(value = "sortOrder" , required = false) String sortOrder,
    		Model uiModel) {
    	Faculty faculty = Faculty.findFaculty(id);
    	List<AghGroup> aghGroups = new ArrayList<AghGroup>();
    	if(faculty!=null){    		
    		List<Course> courses = Course.findCoursesByFaculty(faculty).getResultList();
    		for(Course course : courses){
    			aghGroups.addAll(AghGroup.findAghGroupsByCourse(course).getResultList());
    		}
    		
    		if(sortFieldName != null){
    			SortTool sortTool = new SortTool();
    			String sortProperty = (sortOrder!=null) ? sortFieldName+":"+sortOrder : sortFieldName;
    		    aghGroups = (List<AghGroup>) sortTool.sort(aghGroups, sortProperty);
    		}
    		
    		if(page!=null || size !=null){
                int sizeNo = size == null ? 10 : size.intValue();
                final int fromIndex = page == null ? 0 : (page.intValue() - 1) * sizeNo;
                int toIndex = fromIndex+sizeNo;
                if(fromIndex >= 0 && toIndex <= aghGroups.size() && fromIndex <= toIndex){
                	aghGroups = aghGroups.subList(fromIndex, toIndex);
                }
                float nrOfPages = (float) AghGroup.countAghGroups() / sizeNo;
                uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
    		}
    	}
        uiModel.addAttribute("faculty", Faculty.findFaculty(id));
        uiModel.addAttribute("aghGroups", aghGroups);
        uiModel.addAttribute("itemId", id);
        return "faculties/show";
    }
    
    @RequestMapping(produces = "text/html")
    public String list(
    		@RequestParam(value = "page", required = false) Integer page, 
    		@RequestParam(value = "size", required = false) Integer size, 
    		Model uiModel) {
        if (page != null || size != null) {
            int sizeNo = size == null ? 10 : size.intValue();
            final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
            uiModel.addAttribute("facultys", Faculty.findFacultyEntries(firstResult, sizeNo));
            float nrOfPages = (float) Faculty.countFacultys() / sizeNo;
            uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
        } else {
            uiModel.addAttribute("faculties", Faculty.findAllFacultys());
        }
        return "faculties/list";
    }
}
